#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
int n,m;
int dis[10086];
vector <int> map;
queue<int> lk; 
read(){
	cin>>n>>m;
	for(int x,i=1;i<=n*m;i++){
		cin>>x;
		map.push_back(x);
	}
}
int s;
void solve(){
	dis[0]=1;
	lk.push(0);
	while(!lk.empty()){
		int x=lk.front();
		lk.pop();
		if(map[x+1]>map[x])dis[x+1]=dis[x]+1;
		if(map[x-1]>map[x])dis[x-1]=dis[x]+1;
		if(map[x+m]>map[x])dis[x+m]=dis[x]+1;
		if(map[x-m]>map[x])dis[x-m]=dis[x]+1;
		if(x==0){
			lk.push(x+1);
			lk.push(x+m);
		}
		else if(x>=1&&x<=m-1){
			lk.push(x+1);
			lk.push(x-1);
			lk.push(x+m);
		}
		else if(x%m==0){
			lk.push(x+1);
			lk.push(x+m);
			lk.push(x-m); 
		}
		else if((x+1)%m==0){
			lk.push(x-1);
			lk.push(x+m);
			lk.push(x-m); 
		}
		else {
			lk.push(x+1);
			lk.push(x+m);
			lk.push(x-m); 
			lk.push(x-1);
		}
		cout<<x<<endl;
	}
	cout<<1;
}
int main(){
	read();
	solve();
}
